package com.study.ssm.annotation02.config2;

import com.study.ssm.annotation02.bean.Customer;
import com.study.ssm.annotation02.injection.UserDAO;
import com.study.ssm.annotation02.injection.UserDAOImpl;
import com.study.ssm.annotation02.injection.UserService;
import com.study.ssm.annotation02.injection.UserServiceImpl;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

@Configuration
/*
<context:property-placeholder location="classpath:mybatis/db.properties"/>
* */
@PropertySource("classpath:/annotation02/init.properties")
public class AppConfig1 {

    @Value("${id}")
    private Integer id;
    @Value("${name}")
    private String name;

    @Bean
    public UserDAO userDAO() {
        return new UserDAOImpl();
    }

    /*@Bean
    public UserService userService(UserDAO userDAO) {
        UserServiceImpl userService = new UserServiceImpl();
        userService.setUserDAO(userDAO);
        return userService;
    }*/

    @Bean
    public UserService userService() {
        UserServiceImpl userService = new UserServiceImpl();
        // @Configuration 其实使用了代理模式 每个@Bean注释的方法都增加了额外功能：创建次数 是否再次创建
        // 所以这里不会再次调用userDAO()创建bean
        userService.setUserDAO(userDAO());
        return userService;
    }

    @Bean
    public Customer customer() {
        Customer customer = new Customer();
        customer.setId(id);
        customer.setName(name);

        return customer;
    }


}
